$(function(){

	var customerId = $("#get_customer_id").val();
	var pagePath = $("#pagePath").val();
	var receiver_info_list = $("#receiver_info_list");  //定位坐标
	var receiverInfoAjaxOperator = new ReceiverInfoAjaxOperator();
	var receiverInfoRenderer = new ReceiverInfoRenderer();
	var currentReceiverInfo = new ReceiverInfo();
	var currentReceiverInfoId = '';

	// 初始化函数
	init();

	// 收货信息
    function ReceiverInfo(){
    	var id;
		var address;
		var name;
		var phone;
	};	


	function ReceiverInfoAjaxOperator(){
		var obj = {};

		obj.loadReceiverInfos = function(){
			$.ajax({
				type:"GET",
				url: pagePath+"/receiverInfo/customerId/"+customerId,
				dataType: "json"
			}).done(function(receiverInfos){
				receiverInfoRenderer.render(receiverInfos);
			});
		}

		obj.updateReceiverInfos = function(receiverInfo, onSuccess){
			$.ajax({
				type: "PATCH",
				url: pagePath+"/receiverInfo",
				dataType: "json",
				data: JSON.stringify(receiverInfo),
				contentType: "application/json"
			}).done(function(receiverInfos){
				onSuccess(receiverInfos);
				$("#update_receiver_info").modal('hide');
			})
		}

		obj.removeReceiverInfo = function(receiverInfoId, onSuccess){
			$.ajax({
				type: "DELETE",
				url: pagePath + "/receiverInfo/customerId/"+customerId+"/receiverInfoId/"+receiverInfoId,
				dataType: 'json'
			}).done(function(receiverInfos){
				onSuccess(receiverInfos);
			})
		}

		obj.addReceiverInfo = function(receiverInfo, onSuccess){
			$.ajax({
				type: "POST",
				url: pagePath+"/receiverInfo",
				data: JSON.stringify(receiverInfo),
				dataType: "json",
				contentType: "application/json"
			}).done(function(receiverInfos){
				onSuccess(receiverInfos);
			})
		}

		return obj;
	};


	function ReceiverInfoRenderer(){
		var obj = {};

		obj.render = function(receiverInfos){
			receiver_info_list.empty();
			let tital = '<tr align="center">'+
                            '<td>序号</td>'+
                            '<td>收货人</td>'+
                            '<td>电话号码</td>'+
                            '<td>地址信息</td>'+
                            '<td>修改</td>'+
                            '<td>删除</td>'+
                        '</tr>';
            receiver_info_list.append($.parseHTML(tital));            
			$.each(receiverInfos, function(index, receiverInfo){
				var receiverInfoHtml = 	'<tr align="center">'+
										'<td id='+receiverInfo.id+'>'+eval(index+1)+'</td>'+
										'<td>'+receiverInfo.name+'</td>'+
			                            '<td>'+receiverInfo.phone+'</td>'+
			                            '<td>'+receiverInfo.address+'</td>'+
			                            '<td>'+'<button， id="update_receiver_info_btn" data-target="#update_receiver_info_Model" data-toggle="modal">修改</button>'+'</td>'+
			                            '<td>'+'<button id="delete_receiver_info_btn" data-target="#delete_receiver_info_Model" data-toggle="modal">删除</button>'+'</td>';
			                            '</tr>'
				receiver_info_list.append($.parseHTML(receiverInfoHtml));	
			})
		}


		return obj;
	}


	//修改
	receiver_info_list.on("click", function(event){
		if(event.target.id == "update_receiver_info_btn"){
			currentReceiverInfo.id = $(event.target).parent().prev().prev().prev().prev().attr("id");
			currentReceiverInfo.name = $(event.target).parent().prev().prev().prev().text();
			currentReceiverInfo.address = $(event.target).parent().prev().text();
			currentReceiverInfo.phone = $(event.target).parent().prev().prev().text();

			currentReceiverInfoId = currentReceiverInfo.id;
			//赋值
			$("#new_receiver_info_name").val(currentReceiverInfo.name);
			$("#new_receiver_info_phone").val(currentReceiverInfo.phone);
			$("#new_receiver_info_address").val(currentReceiverInfo.address);
			$("#update_receiver_info").modal('show');

		}

	});


	//删除
	receiver_info_list.on("click", function(event){
		if(event.target.id == "delete_receiver_info_btn"){
			var receiverInfoId = $(event.target).parent().prev().prev().prev().prev().prev().attr("id");
			var isRemove = confirm("是否确认删除?");
			if(isRemove == true){
				receiverInfoAjaxOperator.removeReceiverInfo(receiverInfoId);
				$(event.target).parent().parent().remove();
			}else{

			}
		}
	});


	$("#confirm_update_receiver_info").on("click", function(){
		var currentReceiverInfo = new ReceiverInfo();
		currentReceiverInfo.id = currentReceiverInfoId;
		currentReceiverInfo.address = $("#new_receiver_info_address").val();
		currentReceiverInfo.phone = $("#new_receiver_info_phone").val();
		currentReceiverInfo.name = $("#new_receiver_info_name").val();;
		receiverInfoAjaxOperator.updateReceiverInfos(currentReceiverInfo, receiverInfoRenderer.render);
	});

	$("#add_new_receiver_info_label").on("click", function(){
		$("#add_receiver_info_name").val("");
		$("#add_receiver_info_address").val("")
		 $("#add_receiver_info_phone").val("");
		$("#add_receiver_info").modal('show');
	})


	$("#confirm_add_receiver_info").on("click", function(){
		var currentReceiverInfo = new ReceiverInfo();
		currentReceiverInfo.id = "";
		currentReceiverInfo.address = $("#add_receiver_info_address").val();
		currentReceiverInfo.phone = $("#add_receiver_info_phone").val();
		currentReceiverInfo.name = $("#add_receiver_info_name").val();;
		receiverInfoAjaxOperator.addReceiverInfo(currentReceiverInfo, receiverInfoRenderer.render);
		$("#add_receiver_info").modal('hide');
	});



	// 初始化加载页面
	function init(){
		receiverInfoAjaxOperator.loadReceiverInfos();
	}
});